local bazhen = fk.CreateSkill {

  name = "joy__bazhen",

  tags = { Skill.Compulsory, },

}



bazhen:addEffect(fk.AskForCardUse, {
  name = "joy__bazhen",
  events = {fk.AskForCardUse, fk.AskForCardResponse, fk.FinishJudge},
  frequency = Skill.Compulsory,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(bazhen.name) then
      if event ~= fk.FinishJudge then
        return not player:isFakeSkill(self) and not player:getEquipment(Card.SubtypeArmor) and player:getMark(fk.MarkArmorNullified) == 0
        and (data.cardName == "jink" or (data.pattern and Exppattern:Parse(data.pattern):matchExp("jink|0|nosuit|none")))
      else
        return data.reason == "eight_diagram" and data.card.color ~= Card.Red
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return event == fk.FinishJudge or player.room:askForSkillInvoke(player, bazhen.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(bazhen.name)
    if event == fk.FinishJudge then
      room:notifySkillInvoked(player, bazhen.name, "drawcard")
      player:drawCards(1, bazhen.name)
    else
      room:notifySkillInvoked(player, bazhen.name, "defensive")
      local judgeData = {
        who = player,
        reason = "eight_diagram",
        pattern = ".|.|heart,diamond",
      }
      room:judge(judgeData)
      if judgeData.card.color == Card.Red then
        local card = Fk:cloneCard("jink")
        card.skillName = "eight_diagram"
        card.skillName = "bazhen"
        if event == fk.AskForCardUse then
          data.result = { from = player.id, card = card }
          if data.eventData then
            data.result.toCard = data.eventData.toCard
            data.result.responseToEvent = data.eventData.responseToEvent
          end
        else
          data.result = card
        end
        return true
      end
    end
  end
})
bazhen:addEffect(fk.AskForCardResponse, {
  name = "joy__bazhen",
  events = {fk.AskForCardUse, fk.AskForCardResponse, fk.FinishJudge},
  frequency = Skill.Compulsory,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(bazhen.name) then
      if event ~= fk.FinishJudge then
        return not player:isFakeSkill(self) and not player:getEquipment(Card.SubtypeArmor) and player:getMark(fk.MarkArmorNullified) == 0
        and (data.cardName == "jink" or (data.pattern and Exppattern:Parse(data.pattern):matchExp("jink|0|nosuit|none")))
      else
        return data.reason == "eight_diagram" and data.card.color ~= Card.Red
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return event == fk.FinishJudge or player.room:askForSkillInvoke(player, bazhen.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(bazhen.name)
    if event == fk.FinishJudge then
      room:notifySkillInvoked(player, bazhen.name, "drawcard")
      player:drawCards(1, bazhen.name)
    else
      room:notifySkillInvoked(player, bazhen.name, "defensive")
      local judgeData = {
        who = player,
        reason = "eight_diagram",
        pattern = ".|.|heart,diamond",
      }
      room:judge(judgeData)
      if judgeData.card.color == Card.Red then
        local card = Fk:cloneCard("jink")
        card.skillName = "eight_diagram"
        card.skillName = "bazhen"
        if event == fk.AskForCardUse then
          data.result = { from = player.id, card = card }
          if data.eventData then
            data.result.toCard = data.eventData.toCard
            data.result.responseToEvent = data.eventData.responseToEvent
          end
        else
          data.result = card
        end
        return true
      end
    end
  end
})
bazhen:addEffect(fk.FinishJudge, {
  name = "joy__bazhen",
  events = {fk.AskForCardUse, fk.AskForCardResponse, fk.FinishJudge},
  frequency = Skill.Compulsory,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(bazhen.name) then
      if event ~= fk.FinishJudge then
        return not player:isFakeSkill(self) and not player:getEquipment(Card.SubtypeArmor) and player:getMark(fk.MarkArmorNullified) == 0
        and (data.cardName == "jink" or (data.pattern and Exppattern:Parse(data.pattern):matchExp("jink|0|nosuit|none")))
      else
        return data.reason == "eight_diagram" and data.card.color ~= Card.Red
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return event == fk.FinishJudge or player.room:askForSkillInvoke(player, bazhen.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(bazhen.name)
    if event == fk.FinishJudge then
      room:notifySkillInvoked(player, bazhen.name, "drawcard")
      player:drawCards(1, bazhen.name)
    else
      room:notifySkillInvoked(player, bazhen.name, "defensive")
      local judgeData = {
        who = player,
        reason = "eight_diagram",
        pattern = ".|.|heart,diamond",
      }
      room:judge(judgeData)
      if judgeData.card.color == Card.Red then
        local card = Fk:cloneCard("jink")
        card.skillName = "eight_diagram"
        card.skillName = "bazhen"
        if event == fk.AskForCardUse then
          data.result = { from = player.id, card = card }
          if data.eventData then
            data.result.toCard = data.eventData.toCard
            data.result.responseToEvent = data.eventData.responseToEvent
          end
        else
          data.result = card
        end
        return true
      end
    end
  end
})

return bazhen